package com.ye.controller;

import com.ye.entity.User;
import com.ye.service.UserService;
import com.ye.utils.OssUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@RestController
@RequestMapping("/userInfo")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 通过用户名获取用户信息
     *
     * @param user
     * @return
     */
    @RequestMapping("/getUserByName")
    public User getUserByUserName(@RequestBody User user) {
        return userService.getUserByUserName(user);
    }

    /**
     * 通过用户名修改密码
     *
     * @param user
     * @return
     */
    @RequestMapping("/editPasswordByName")
    public Integer editPasswordByName(@RequestBody User user) {
        return userService.editPassword(user);
    }

    /**
     * 通过用户名修改用户信息
     *
     * @param user
     * @return
     */
    @RequestMapping("/editUserInfoByName")
    public Integer editUserInfo(@RequestBody User user) {
        return userService.editUserInfo(user);
    }

    /**
     * 通过用户名修改用户状态
     *
     * @param user
     * @return
     */
    @RequestMapping("/editStatusByName")

    public Integer editStatus(@RequestBody User user) {
        return userService.editStatus(user);
    }

    @RequestMapping("/updateSignature")
    public Integer updateSignature(@RequestBody User user) {
        return userService.updateSignature(user);

    }

    @RequestMapping("/uploadLogo")
    public String upload(HttpServletRequest request) throws IOException {
//        获取文件
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multiFile = multipartRequest.getFile("file");
//获取文件名
        assert multiFile != null;
        String filename = multiFile.getOriginalFilename();
        assert filename != null;
        File temporary = new File(filename);

        try {
//      multipartFile转File
            FileUtils.copyInputStreamToFile(multiFile.getInputStream(), temporary);
        } catch (IOException e) {
            e.printStackTrace();
        }

//        远程图片地址
        String remoteUrl = returnImgRemoteUrl(filename, temporary);



        return remoteUrl;
    }

    /**
     * @param fileName 文件名
     * @param file     文件
     * @return remoteUrl
     */
    private String returnImgRemoteUrl(String fileName, File file) {
        InputStream fileInputStream = null;
        try {
//            获取文件输入流
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//        调用封装好的的oss上传工具，返回得到url地址
        return OssUtil.startUpload(fileName, fileInputStream, 2);

    }


}
